/*
 * @Author: boyyang
 * @Date: 2022-01-05 09:20:58
 * @LastEditTime: 2022-02-23 17:51:25
 * @LastEditors: boyyang
 * @Description: 深拷贝
 * @FilePath: \boyyang\src\utils\deepClone.ts
 */

// 判断是否为数组
const isArr = (origin: any): boolean => {
    let str = '[object Array]'
    return Object.prototype.toString.call(origin) == str ? true : false
}

const deepClone = <T>(origin: T, target?: Record<string, any> | T): T => {
    let tar = target || {}

    for (const key in origin) {
        if (Object.prototype.hasOwnProperty.call(origin, key)) {
            if (typeof origin[key] === 'object' && typeof origin[key] !== null) {
                tar[key] = isArr(origin[key]) ? [] : {}
                deepClone(origin[key], tar[key])
            } else {
                tar[key] = origin[key]
            }
        }
    }

    return tar as T
}

export {
    deepClone
}


